package pe.edu.sistemas.sisdiho.entities;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class Mailing
{
	final String senderEmailID;
	final String senderPassword;
	final String emailSMTPserver = "smtp.gmail.com";
	final String emailServerPort = "465";
	
	public Mailing(String senderEmailId, String senderEmailPassword)
	{
		this.senderEmailID=senderEmailId;
		this.senderPassword=senderEmailPassword;
	}
	public void EnviarCorreo(String receiverEmailID, String emailSubject, String emailBody)	
	{
		Properties props = new Properties();
		props.put("mail.smtp.user",senderEmailID);
		props.put("mail.smtp.host", emailSMTPserver);
		props.put("mail.smtp.port", emailServerPort);
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.auth", "true");
		// props.put("mail.smtp.debug", "true");
		props.put("mail.smtp.socketFactory.port", emailServerPort);
		props.put("mail.smtp.socketFactory.class",
		"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		
		SecurityManager security = System.getSecurityManager();
		
		try
		{
			Authenticator auth = new SMTPAuthenticator();
			Session session = Session.getInstance(props, auth);
			// session.setDebug(true);
			
			MimeMessage msg = new MimeMessage(session);
			msg.setText(emailBody);
			msg.setSubject(emailSubject);
			msg.setFrom(new InternetAddress(senderEmailID));
			msg.addRecipient(Message.RecipientType.TO,
			new InternetAddress(receiverEmailID));
			Transport.send(msg);
		}
		catch (Exception mex)
		{
			mex.printStackTrace();
			System.out.println("NO SE PUDO ENVIAR EL CORREO");
		}
	}
	
	public class SMTPAuthenticator extends javax.mail.Authenticator
	{
	public PasswordAuthentication getPasswordAuthentication()
		{
			return new PasswordAuthentication(senderEmailID, senderPassword);
		}
	}
}